﻿using BCCommon;
using BCData.TMS.Driver.DriverExamine;
using BCDto.TMS.Driver.DriverExamine;
using BCEntity.TMS.Driver.DriverExamine;
using BCService.TMS.Driver.DriverExamine;
using BCService.Utils;
using System;
using System.Collections.Generic;

namespace ATSimService.NewLogicService.AdminPanelService.DriverExamine
{
    public class DriverExamineMarkService : IDriverExamineMarkService
    {
        protected readonly IDriverExamineMarkData driverExamineMarkData;
        protected readonly IRedisService redisService;
        protected readonly string entityName = "DriverExamineMarkEntity";

        public DriverExamineMarkService(IDriverExamineMarkData driverExamineMarkData, IRedisService redisService)
        {
            this.driverExamineMarkData = driverExamineMarkData;
            this.redisService = redisService;
        }

        public long Add(DriverExamineMarkRequestDto dto)
        {
            var entity = dto.As<DriverExamineMarkEntity>();
            return driverExamineMarkData.InsertAsync(entity).GetAwaiter().GetResult();
        }

        public void Delete(long examineMarkId)
        {
            if (!driverExamineMarkData.CheckDriverIdInUsedAsync(examineMarkId).GetAwaiter().GetResult())
            {
                throw new ArgumentException("ExamineMarkDriverIdInUsed");
            }

            if (!driverExamineMarkData.CheckPaperIdInUsedAsync(examineMarkId).GetAwaiter().GetResult())
            {
                throw new ArgumentException("PaperIdInUsed");
            }


            bool result = driverExamineMarkData.DeleteAsync(examineMarkId).GetAwaiter().GetResult();
            if (result)
            {
                string key = string.Format("{0}@ExamineMarkId:{1}", entityName, examineMarkId);
                redisService.Delete(key);
            }
        }

        public DriverExamineMarkDto GetById(long examineMarkId)
        {
            string key = string.Format("{0}@ExamineMarkId:{1}", entityName, examineMarkId);

            DriverExamineMarkEntity entity = this.redisService.TryGet(key, () => driverExamineMarkData.GetDriverExamineMarkByIdAsync(examineMarkId).GetAwaiter().GetResult());

            return entity.As<DriverExamineMarkDto>();
        }

        public PageableList<DriverExamineMarkDto> GetByPaging(int pageIndex, int pageSize)
        {
            var queryResult = driverExamineMarkData.GetDriverExamineMarksAsync(pageIndex, pageSize).GetAwaiter().GetResult();

            return new PageableList<DriverExamineMarkDto>
            {
                Items = queryResult.Item1.As<IEnumerable<DriverExamineMarkDto>>(),
                Count = queryResult.Item2,
                PageIndex = pageIndex,
                PageSize = pageSize
            };
        }

        public DriverExamineMarkDto Update(long examineMarkId, DriverExamineMarkRequestDto dto)
        {
            string key = string.Format("{0}@ExamineMarkId:{1}", entityName, examineMarkId);
            var entity = dto.As<DriverExamineMarkEntity>();
            entity.ExamineMarkId = examineMarkId;
            if (!driverExamineMarkData.CheckExistsByIdAsync(examineMarkId).GetAwaiter().GetResult())
            {
                throw new ArgumentException("考试成绩信息不存在");
            }
            var result = driverExamineMarkData.UpdateAsync(entity).GetAwaiter().GetResult();
            this.redisService.Delete(key);
            return result.As<DriverExamineMarkDto>();
        }
    }
}
